import { useUserStore } from '@/stores/user'

export const permissionDirective = {
    mounted(el, binding) {
        checkPermission(el, binding)
    },
    updated(el, binding) {
        checkPermission(el, binding)
    }
}
function checkPermission(el, binding) {
    const authStore = useUserStore()
    const requiredPermissions = binding.value

    if (!hasPermission(authStore.menus, requiredPermissions)) {
        el.style.display = 'none'
        // 或者完全移除
        // el.parentNode?.removeChild(el)
    } else {
        el.style.display = ''
    }
}
function hasPermission(userPermissions, requiredPermissions) {
    // 如果requiredPermissions是数组，检查用户是否有任一权限
    if (Array.isArray(requiredPermissions)) {
        return requiredPermissions.some(perm => userPermissions.includes(perm))
    }
    // 如果是字符串，直接检查
    return userPermissions.includes(requiredPermissions)
}
